<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>

<body>
    <h1>基本使用</h1>
    <button>发送GET</button>
    <button>发送POST</button>
    <button>发送PUT</button>
    <button>发送DELETE</button>
    <button>更改评分</button>
    <script>
        const btns = document.getElementsByTagName('button');
        axios.defaults.method = 'GET'//设置默认请求方法为GET
        axios.defaults.baseURL = 'http://localhost:3000';
        let user = {};
        btns[0].onclick = function () {
            //发送ajax请求
            axios({
                url: '/users/1'
            }).then((response) => {
                user = response.data;
                console.log('------请求得到的评分------------');
                console.log(user.userrate[0].hotmovie[0].rate);
            })
        }
        btns[1].onclick = function () {
            //发送ajax请求
            axios({
                // 请求类型
                method: 'POST',
                // url
                url: '/users',
                // 设置请求体
                data: {
                    username: '1leven',
                    password: '123'
                }
            }).then((response) => {
                console.log(response.data);
            })
        }
        // 更改评分
        btns[4].onclick = function () {
            user.userrate[0].hotmovie[0].rate = 4
            console.log('-----评分更改为---------');
            console.log(user.userrate[0].hotmovie[0].rate);
        }
        btns[2].onclick = function () {
            //发送ajax请求
            axios({
                // 请求类型
                method: 'PUT',
                // url
                url: '/users/1',
                // 设置请求体
                data: user,
            }).then((response) => {
                console.log(response.data);
            })
        }
        btns[3].onclick = function () {
            //发送ajax请求
            axios({
                // 请求类型
                method: 'DELETE',
                // url
                url: '/posts/3',
            }).then((response) => {
                console.log(response);
            })
        }
        const json = {
            "userrate": [
                {
                    "hotmovie": [
                        {
                            "id": 1,
                            "rate": 0
                        },
                        {
                            "id": 2,
                            "rate": 0
                        },
                        {
                            "id": 3,
                            "rate": 0
                        },
                        {
                            "id": 4,
                            "rate": 0
                        },
                        {
                            "id": 5,
                            "rate": 0
                        },
                        {
                            "id": 6,
                            "rate": 0
                        },
                        {
                            "id": 7,
                            "rate": 0
                        },
                        {
                            "id": 8,
                            "rate": 0
                        },
                        {
                            "id": 9,
                            "rate": 0
                        },
                        {
                            "id": 10,
                            "rate": 0
                        }
                    ]
                },
                {
                    "hotmovie2": [
                        {
                            "id": 1,
                            "rate": 0
                        },
                        {
                            "id": 2,
                            "rate": 0
                        },
                        {
                            "id": 3,
                            "rate": 0
                        },
                        {
                            "id": 4,
                            "rate": 0
                        },
                        {
                            "id": 5,
                            "rate": 0
                        }
                    ]
                }
            ]
        }
        // data = JSON.parse(json);
        console.log(json);
    </script>
</body>

</html>